var app = getApp()
var IP = app.globalData.IP
var dataformat = require('../../../utils/dataformat')
Page({
  data: {
    active: "2",
    orderlist: [],
    serviceinfo: [],
    employeeid: wx.getStorageSync('employeeid'),
  },

  onLoad(options) {
    if (options?.active == '2') {
      this.getorderlistbystatus(options.active)
      this.onLoad()
      console.log('aaaa')
    } else {
      this.setData({
        // active: Number(options.active)

        active: options?.active
      })
      console.log("ssssssssssssssss")


    }


  },

  onChange(event) {
    var status = event.detail.name

    this.getorderlistbystatus(status)




  },

  call(e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.telnumber, //仅为示例，并非真实的电话号码
      success() {

      },
    })

  },
  //确认到达
  arriveDestination(e) {

    wx.showModal({
      title: '',
      content: '确认到达目的地？',
      complete: (res) => {
        if (res.cancel) {

        }

        if (res.confirm) {
          wx.request({
            url: `http://${IP}:8080/order/updateStatusTo6/` + e.currentTarget.dataset.id,
            success: res => {

              this.onLoad()

            }
          })
        }
      }
    })


  },


  //获取不同状态订单 statu=2已接单  4已完成  5 已取消  6已上门
  getorderlistbystatus(status) {
    var employeeid=this.data.employeeid
    
    wx.request({
      url: `http://${IP}:8080/order/getorderlistbystatusAndemployeeid/${status}/${employeeid}`,
      success: res => {
        this.setData({
          orderlist: res.data
        })

        // 格式化时间
        this.data.orderlist.forEach((item) => {
          item.ordertime = dataformat.formatTime(item.ordertime, 'YY-MM-DD hh-mm')
        })
        this.setData({
          orderlist: this.data.orderlist
        })
        console.log(this.data.orderlist)



        // console.log(this.data.orderlist)
        //根据获取的id依次同步请求获取服务图片
        this.getserviceImage()
      }
    })


  },


  //同步请求获取对应服务图片
  async getserviceImage() {
    this.setData({
      serviceinfo: []
    })
    var list = this.data.orderlist
    for (var i = 0; i < list.length; i++) {
      await new Promise((resolve, reject) => {
        wx.request({
          url: `http://${IP}:8080/service/getservicebyserviceid/` + list[i].serviceid,
          success: res => {
            this.setData({
              serviceinfo: this.data.serviceinfo.concat(res.data),
            })
            console.log(this.data.serviceinfo)
            resolve();
          }
        })

      })

    }

  },


})